#include "Player.h"
#include "Card.h"
#include <iostream>
#include <string.h>
#include <QtCore>

using namespace std;
Player::Player(Hand h, int i,game* g,Client* c)
{
    client=c;
    localHand=h;
    id=i;
    gui=g;
    partner=NULL;
    //localHand.updateOwner(i);

}

void Player::getMove(bool a)
{
    //interface with GUI
    gui->getMove(a);
}
void Player::sendHand()
{
    qDebug()<<"Send hand";
    gui->changeSouthHand(&localHand);
     qDebug()<<"done send hand";
}

void Player::getBid()
{
    qDebug()<<"Start send bid";
    string f=gui->getbid();//gui interface


}
Hand Player::getValidMoves(char)
{
    qDebug()<<"get valid moves in player";
}

void Player::startDummy()
{

}
void Player::sendScoreUpdate(QString wa,QString wb,QString ta,QString tb)
{
    gui->set_score(wa, wb, ta, tb);
}
void Player::sendGameWinner(QString winner)
{
    gui->gameWinner(winner);
}
void  Player::sendGameStatus(QStringList a)
{
    qDebug() << a;
}

string Player::getUsername()
{
    return username;
}

bool Player::setUsername(string usr)
{
    username=usr;
    return true;
}

bool Player::setLocalHand(Hand h)
{
    localHand=h;

}

Player* Player::getPartner()
{
    return partner;
}

void Player::sendBid(QStringList b, int player)
{
    qDebug()<<"send bid";
    QString str;
    str.append(b.at(0));
    str.append(" ");
    str.append(b.at(1));
    str.append(" ");
    str.append(b.at(2));
    str.append(b.at(3));
    gui->sendBid(str);
}

void Player::sendMove(QStringList list, int player)
{
    //QString username = list.at(0);
    string temp = list.at(1).toUtf8().constData();
    char suit = temp[0];
    string value = list.at(2).toUtf8().constData();

    Card card(suit, 0, player, value); // should ID be player? player is the id of the client who played the card

    gui->displaymove(card, player);
    gui->changeSouthHand(&localHand);

    localHand.removeCard(card);
    if(partner!=NULL)
    {
     partner->localHand.removeCard(card);
     gui->show_Dummy(&partner->localHand);
    }
}
void Player::print()
{
    qDebug()<<"===================================\n";
      qDebug()<<"player id = "<<id<<"\n";
      qDebug()<<"player username = "<<QString::fromStdString(username)<<"\n";
      qDebug()<<"player hand \n";
    localHand.print();
      qDebug()<<"Declarer = "<<declarer<<"\n";
    //cout<<"Highest bid "<<getHighestBid()<<"\n";
      qDebug()<<"===================================\n";
}


void Player::startDealing()
{
    qDebug()<<"call StartDealing";
    gui->startDealing();
    gui->newGame();
}

void Player::startRedealing()
{

}

void Player::startBidding()
{

    gui->startBidding();
}

void Player::startPlay()
{

}

void Player::sendTrickWinner(QString winner)
{
    qDebug()<<"send trik winner from player";
    gui->endTrick();
    gui->trickWinner(winner);
}

void Player::sendContract(QStringList contract)
{
    qDebug()<<contract;
    QString string;
    string.append(contract.at(0));
    string.append(contract.at(1));
    gui->sendContract(string);
}

void Player::sendDummyId(int dummy)
{
    switch (id) {
    case 0:dummy+=2;
        break;
    case 1:dummy+=1;
        break;
    case 2:break;
    case 3:dummy-=1;
        break;
    }
        gui->setDummy(dummy);
}

void Player::sendDummyCards(Hand dHand)
{
    qDebug()<<"=================   Create a new dummy";
    partner=new Player(dHand,0,gui,client);
    partner->print();
    gui->show_Dummy(&dHand);
}

bool Player::hasCardsInSuit(char suit)
{
    if(localHand.numberOfSuit(suit)>0)
        return true;
    return false;
}

